<?php
namespace App\Controllers\Home;
use \App\Models\Home\User;
/**
*	设置用户
*/
class SetUser extends \App\Libs\Base\BaseAction
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		$key = '';
		if (isset($_COOKIE[SESSION_KEY_OPEN]) && ! empty($_COOKIE[SESSION_KEY_OPEN])) {
            $key = trim($_COOKIE[SESSION_KEY_OPEN]);
		}
		
		$openid = \App\Libs\Cache\Redis::get(trim($key));
		if( empty( $openid ) ){
				return \App\Libs\Views\Json::view(1, '授权失败，请退出重新进入', null);	
		}
		$is_user = (int)$this->request->get('is_user');
		if( in_array( $is_user , [1,2]) ){
			$user = User::find(array(
				' openid = :openid:',
				'bind'=>array( 'openid'=>$openid )
			));
			if( !empty( $user ) ){
				if( in_array( $user->is_user , [1,2]) ){
					return \App\Libs\Views\Json::view(0, '已选择', null);	
				}
				$user->is_user = $is_user;
				$user->utime = date('Y-m-d H:i:s');
				$user->save();
				return \App\Libs\Views\Json::view(0, '选择成功', null);	
			}else{
				return \App\Libs\Views\Json::view(1, '用户不存在，请重新进入选择', null);	
			}
		}else{
			return \App\Libs\Views\Json::view(1, '用户不存在，请重新进入选择', null);	
		}
		
		
	}
	
	
}
